home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / Apache 1.0 / src / mod_alias.c < prev    next >
Text File  |  1995-12-04  |  8KB  |  245 lines

  1.  
  2. /* ====================================================================
  3.  * Copyright (c) 1995 The Apache Group.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * 1. Redistributions of source code must retain the above copyright
  10.  *    notice, this list of conditions and the following disclaimer. 
  11.  *
  12.  * 2. Redistributions in binary form must reproduce the above copyright
  13.  *    notice, this list of conditions and the following disclaimer in
  14.  *    the documentation and/or other materials provided with the
  15.  *    distribution.
  16.  *
  17.  * 3. All advertising materials mentioning features or use of this
  18.  *    software must display the following acknowledgment:
  19.  *    "This product includes software developed by the Apache Group
  20.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  21.  *
  22.  * 4. The names "Apache Server" and "Apache Group" must not be used to
  23.  *    endorse or promote products derived from this software without
  24.  *    prior written permission.
  25.  *
  26.  * 5. Redistributions of any form whatsoever must retain the following
  27.  *    acknowledgment:
  28.  *    "This product includes software developed by the Apache Group
  29.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  30.  *
  31.  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
  32.  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  33.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
  35.  * IT'S CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  39.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  42.  * OF THE POSSIBILITY OF SUCH DAMAGE.
  43.  * ====================================================================
  44.  *
  45.  * This software consists of voluntary contributions made by many
  46.  * individuals on behalf of the Apache Group and was originally based
  47.  * on public domain software written at the National Center for
  48.  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
  49.  * For more information on the Apache Group and the Apache HTTP server
  50.  * project, please see <http://www.apache.org/>.
  51.  *
  52.  */
  53.  
  54.  
  55. /*
  56.  * http_alias.c: Stuff for dealing with directory aliases
  57.  * 
  58.  * Original by Rob McCool, rewritten in succession by David Robinson
  59.  * and rst.
  60.  * 
  61.  */
  62.  
  63. #include "httpd.h"
  64. #include "http_config.h"
  65.  
  66. typedef struct {
  67.     char *real;
  68.     char *fake;
  69.     char *forced_type;
  70. } alias_entry;
  71.  
  72. typedef struct {
  73.     array_header *aliases;
  74.     array_header *redirects;
  75. } alias_server_conf;
  76.  
  77. module alias_module;
  78.  
  79. void *create_alias_config (pool *p, server_rec *s)
  80. {
  81.     alias_server_conf *a =
  82.       (alias_server_conf *)pcalloc (p, sizeof(alias_server_conf));
  83.  
  84.     a->aliases = make_array (p, 20, sizeof(alias_entry));
  85.     a->redirects = make_array (p, 20, sizeof(alias_entry));
  86.     return a;
  87. }
  88.  
  89. void *merge_alias_config (pool *p, void *basev, void *overridesv)
  90. {
  91.     alias_server_conf *a =
  92.     (alias_server_conf *)pcalloc (p, sizeof(alias_server_conf));
  93.     alias_server_conf *base = (alias_server_conf *)basev,
  94.     *overrides = (alias_server_conf *)overridesv;
  95.  
  96.     a->aliases = append_arrays (p, overrides->aliases, base->aliases);
  97.     a->redirects = append_arrays (p, overrides->redirects, base->redirects);
  98.     return a;
  99. }
  100.  
  101. char *add_alias(cmd_parms *cmd, void *dummy, char *f, char *r)
  102. {
  103.     server_rec *s = cmd->server;
  104.     alias_server_conf *conf =
  105.         (alias_server_conf *)get_module_config(s->module_config,&alias_module);
  106.     alias_entry *new = push_array (conf->aliases);
  107.  
  108.     /* XX r can NOT be relative to DocumentRoot here... compat bug. */
  109.     
  110.     new->fake = f; new->real = r; new->forced_type = cmd->info;
  111.     return NULL;
  112. }
  113.  
  114. char *add_redirect(cmd_parms *cmd, void *dummy, char *f, char *url)
  115. {
  116.     server_rec *s = cmd->server;
  117.     alias_server_conf *conf =
  118.         (alias_server_conf *)get_module_config(s->module_config,&alias_module);
  119.     alias_entry *new = push_array (conf->redirects);
  120.  
  121.     if (!is_url (url)) return "Redirect to non-URL";
  122.     
  123.     new->fake = f; new->real = url;
  124.     return NULL;
  125. }
  126.  
  127. command_rec alias_cmds[] = {
  128. { "Alias", add_alias, NULL, RSRC_CONF, TAKE2, 
  129.     "a fakename and a realname"},
  130. { "ScriptAlias", add_alias, CGI_MAGIC_TYPE, RSRC_CONF, TAKE2, 
  131.     "a fakename and a realname"},
  132. { "Redirect", add_redirect, NULL, RSRC_CONF, TAKE2, 
  133.     "a document to be redirected, then the destination URL" },
  134. { NULL }
  135. };
  136.  
  137. int alias_matches (char *uri, char *alias_fakename)
  138. {
  139.     char *end_fakename = alias_fakename + strlen (alias_fakename);
  140.     char *aliasp = alias_fakename, *urip = uri;
  141.  
  142.     while (aliasp < end_fakename) {
  143.     if (*aliasp == '/') {
  144.         /* any number of '/' in the alias matches any number in
  145.          * the supplied URI, but there must be at least one...
  146.          */
  147.         if (*urip != '/') return 0;
  148.         
  149.         while (*aliasp == '/') ++ aliasp;
  150.         while (*urip == '/') ++ urip;
  151.     }
  152.     else {
  153.         /* Other characters are compared literally */
  154.         if (*urip++ != *aliasp++) return 0;
  155.     }
  156.     }
  157.  
  158.     /* Check last alias path component matched all the way */
  159.  
  160.     if (aliasp[-1] != '/' && *urip != '\0' && *urip != '/')
  161.     return 0;
  162.  
  163.     /* Return number of characters from URI which matched (may be
  164.      * greater than length of alias, since we may have matched
  165.      * doubled slashes)
  166.      */
  167.  
  168.     return urip - uri;
  169. }
  170.  
  171. char *try_alias_list (request_rec *r, array_header *aliases, int doesc)
  172. {
  173.     alias_entry *entries = (alias_entry *)aliases->elts;
  174.     int i;
  175.     
  176.     for (i = 0; i < aliases->nelts; ++i) {
  177.         alias_entry *p = &entries[i];
  178.         int l = alias_matches (r->uri, p->fake);
  179.  
  180.         if (l > 0) {
  181.         if (p->forced_type)
  182.         table_set (r->notes, "alias-forced-type", p->forced_type);
  183.                
  184.         if (doesc) {
  185.         char *escurl;
  186.         /* would like to use os_escape_path here, but can't */
  187.         escurl = escape_uri(r->pool, r->uri + l);
  188.         return pstrcat(r->pool, p->real, escurl, NULL);
  189.         } else
  190.         return pstrcat(r->pool, p->real, r->uri + l, NULL);
  191.         }
  192.     }
  193.  
  194.     return NULL;
  195. }
  196.  
  197. int translate_alias_redir(request_rec *r)
  198. {
  199.     void *sconf = r->server->module_config;
  200.     alias_server_conf *conf =
  201.         (alias_server_conf *)get_module_config(sconf, &alias_module);
  202.     char *ret;
  203.  
  204.     if (r->uri[0] != '/' && r->uri[0] != '\0') 
  205.         return BAD_REQUEST;
  206.  
  207.     if ((ret = try_alias_list (r, conf->redirects, 1)) != NULL) {
  208.         table_set (r->headers_out, "Location", ret);
  209.         return REDIRECT;
  210.     }
  211.     
  212.     if ((ret = try_alias_list (r, conf->aliases, 0)) != NULL) {
  213.         r->filename = ret;
  214.         return OK;
  215.     }
  216.     
  217.     return DECLINED;
  218. }
  219.  
  220. int type_forced_alias (request_rec *r)
  221. {
  222.     char *t = table_get (r->notes, "alias-forced-type");
  223.     if (!t) return DECLINED;
  224.     r->content_type = t;
  225.     return OK;
  226. }
  227.  
  228. module alias_module = {
  229.    STANDARD_MODULE_STUFF,
  230.    NULL,            /* initializer */
  231.    NULL,            /* dir config creater */
  232.    NULL,            /* dir merger --- default is to override */
  233.    create_alias_config,        /* server config */
  234.    merge_alias_config,        /* merge server configs */
  235.    alias_cmds,            /* command table */
  236.    NULL,            /* handlers */
  237.    translate_alias_redir,    /* filename translation */
  238.    NULL,            /* check_user_id */
  239.    NULL,            /* check auth */
  240.    NULL,            /* check access */
  241.    type_forced_alias,        /* type_checker */
  242.    NULL,            /* fixups */
  243.    NULL                /* logger */
  244. };
  245.